﻿using System;
using System.ComponentModel.Composition;
using System.Security.Cryptography;
using System.Windows;
using System.Windows.Controls;
using ParaEditor.Desktop.Interfaces;
using ParaEditor.Infrastructure;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.ServiceLocation;
using ParaEditor.SysMgr.Service.OperatorMgrService.Impl;
using Micropayment.Model;
using ParaEditor.SysMgr.Service.LogMgrService.Impl;
using System.Text;

namespace ParaEditor.Desktop.View
{
    /// <summary>
    /// LoginView.xaml 的交互逻辑
    /// </summary>
    [ViewExport(RegionName = RegionNames.MainWindowRegion, ViewName = ViewNames.LoginViewName)]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class LoginView : UserControl
    {
        LogMgrServiceImpl _log = new LogMgrServiceImpl();
        public LoginView()
        {
            InitializeComponent();
        }

        [Import]
        public ILoginViewModel Model
        {
            get
            {
                return DataContext as ILoginViewModel;
            }
            set
            {
                this.DataContext = value;
            }
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // 在此处添加事件处理程序实现。
            Application.Current.MainWindow.Close();//关闭程序
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            userName.Focus();//用户名输入框获得焦点

        }

        private void Image_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // 在此处添加事件处理程序实现。

        }

        private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // 在此处添加事件处理程序实现。
            //DragMove();//移动窗体
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            OperatorMgrServiceImpl operatorMgrServiceImpl = new OperatorMgrServiceImpl();
            //OPERATOR tco = service.FindByID(userName.Text);
            OPERATOR tco = operatorMgrServiceImpl.GetOperatorByID(userName.Text);
            if (tco == null)
            {
                Microsoft.Windows.Controls.MessageBox.Show("连接数据库失败，请重试！", "连接失败");
                return;
            }

            IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(this.passwordBox.SecurePassword);

            // 使用.NET内部算法把IntPtr指向处的字符集合转换成字符串  
            string password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            string encryptPwd;

            try
            {
                encryptPwd = MD5Helper.PlatformMD5Encrypt(password);
            }
            catch(Exception ex)
            {
                Microsoft.Windows.Controls.MessageBox.Show(ex.Message, "错误");
                return;
            }

            //if(true)
            if (tco != null && encryptPwd.Equals(tco.OPERATOR_PWD))//登录正确
            {
                //OPERATOR tco = new OPERATOR() { RULE_ID = 2, OPERATOR_ID = "000005", OPERATOR_NAME = "测试人员" };
                ApplictionDatacontext.opt = tco;
                IRegion region = Model.RegionManager.Regions[RegionNames.MainWindowRegion];

                object loginView = region.GetView(ViewNames.LoginViewName);
                if (loginView != null)
                {
                    region.Remove(loginView);
                    object mainWindowView = region.GetView(ViewNames.MainWindowViewName);
                    if (mainWindowView == null)
                    {
                        mainWindowView = ServiceLocator.Current.GetInstance((typeof(UserControl)), ViewNames.MainViewName);
                        region.Add(mainWindowView, ViewNames.MainWindowViewName);
                    }

                    region.Activate(mainWindowView);

                    Application.Current.MainWindow.MaxHeight = SystemParameters.WorkArea.Height;
                    Application.Current.MainWindow.WindowState = System.Windows.WindowState.Maximized;
                    //_log.writeLog("一般操作日志", "0", "", "操作员" + tco.OPERATOR_ID + "成功登录");
                }

            }
            else//登录失败
            {
                //errForm messForm = new errForm();
                //messForm.ShowDialog();
                userName.Text = "";
                passwordBox.Password = "";
                userName.Focus();
                Microsoft.Windows.Controls.MessageBox.Show("账号或密码输入错误，登陆失败！请重试！！", "警告");
                //_log.writeLog("一般操作日志", "1", "", "操作员" + tco.OPERATOR_ID + "登录失败");
            }
        }

    }
}
